package com.canbot.u05.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;

import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;

import java.util.HashMap;
import java.util.Iterator;

public class U05RobotService extends Service {

        private static final int MESSAGE_CODE_PLAY = 0;

        private static final int MESSAGE_CODE_START_RECOGNIZER = MESSAGE_CODE_PLAY
                + 1;

        private static final int MESSAGE_CODE_STOP_RECOGNIZER = MESSAGE_CODE_PLAY
                + 2;

        private static final int MESSAGE_CODE_PLAY_TTS = MESSAGE_CODE_PLAY + 3;

        private static final int MESSAGE_CODE_START_ACTIVITY = MESSAGE_CODE_PLAY
                + 4;

        private static final int MESSAGE_CODE_STOP_PHOTO = MESSAGE_CODE_PLAY + 5;

        //    protected static final int MESSAGE_CODE_UU_FM = MESSAGE_CODE_PLAY + 6;
        protected static final int MESSAGE_CLOSE_GAME = MESSAGE_CODE_PLAY + 7;

        private String TAG = "U05RobotService";

//    private boolean completd = false;

        @Override
        public void onCreate() {
                super.onCreate();

                // IntentFilter filter = new IntentFilter();
                // filter.addAction(ReceiverSocketAction.PLAY_SOUND_END);
                // LocalBroadcastManager.newInstance(this).registerReceiver(a, filter);

        }

        @Override
        public IBinder onBind(Intent arg0) {
                return new Messenger(handler).getBinder();
        }

        /**
         * handler 用来跨进程信息的处理.
         */
        Handler handler = new Handler() {
                public void handleMessage(Message msg) {
                        switch (msg.what) {
                                case MESSAGE_CODE_PLAY: {
                                        System.out.println(TAG + " 服务端收到的信息:  msg.what " + msg.what
                                                + " +msg.obj " + msg.obj.toString());
                                        Bundle b2 = (Bundle) msg.obj;
                                        HashMap<Integer, String> map = (HashMap<Integer, String>) b2.getSerializable("key");
                                        Iterator<Integer> keys = map.keySet().iterator();
                                        while (keys.hasNext()) {
                                                int key = keys.next();
                                                MsgSendUtils.sendStringMsg(key, map.get(key));
                                        }
                                        break;
                                }

                                case MESSAGE_CODE_PLAY_TTS: {
                                        Bundle b2 = (Bundle) msg.obj;
                                        String text = b2.getString("text");
                                        MsgSendUtils.sendStringMsg(MsgType.PLAY_TTS,
                                                text);
                                        break;
                                }

//                                case MESSAGE_CODE_START_ACTIVITY: {
//                                        Bundle b2 = (Bundle) msg.obj;
//                                        String text = b2.getString("name");
//                                        MsgSendUtils.sendStringMsg(MsgType.SEND_START_ACTIVITY,
//                                                text);
//                                        break;
//                                }

//                case MESSAGE_CODE_UU_FM:
//                    Bundle b2 = (Bundle) msg.obj;
//                    String fm_action = b2.getString("fm_action");
//
//                    MsgSendUtils.sendStringMsg(MsgType.QINGTINGFM,
//                            fm_action);
//                    break;
//                case MESSAGE_CLOSE_GAME:
//                    MsgSendUtils.sendStringMsg(MsgType.STRING_INFO,
//                            "close_game");
//                    break;
                                default:
                                        break;
                        }
                }

        };

        BroadcastReceiver a = new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
                        // String path = intent.getStringExtra(IntentKey.Sound_Path);
                        Log.e("sound", "PLAY_SOUND_END");
//            completd = true;

                        Message reply = Message.obtain(null, 0);
                        System.out.println(TAG + " 服务端回复信息给客户端 ");
                        Bundle b = new Bundle();
                        b.putString("state", "播放完毕");
                        reply.obj = b;
                        // try {
                        // replyTo.send(reply);
                        // } catch (RemoteException e) {
                        // e.printStackTrace();
                        // }

                }

        };

//    private void sendAction(String action) {
//        System.out.println("sendAction " + action);
//        MsgSendUtils.sendStringMsg(MsgType.SEND_ACTION, action);
//    }
//
//    private void sendEyeMotion(String action) {
//        System.out.println("sendEyeMotion " + action);
//        MsgSendUtils.sendStringMsg(MsgType.SEND_EYE_MOTION, action);
//    }
//
//    private void playSound(String path) {
//        // if(true)
//        // {
//        System.out.println(TAG + "playSound  " + path);
//        // return ;
//        // }
//
//        MsgSendUtils.sendStringMsg(MsgType.SEND_MSGTYPE_PLAY_SOUND, path);
//        // Intent intent = new Intent(ReceiverAPPAction.START_PLAY_SOUND);
//        // intent.putExtra(IntentKey.SOUND_PATH, path);
//        // intent.putExtra(IntentKey.COMPLETED_DO, IntentContent.NONE);
//        // intent.putExtra(IntentKey.MSG_TYPE, MsgType.SEND_MSGTYPE_PLAY_SOUND);
//        // sendBroadcast(intent);
//
//    }

}